﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Adams.Shared
{
    public class Crypt
    {
        private const string _Password = "hacking is illegal. Please don't do it!";

        public static string Encrypt(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below

            using (MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider())
            {
                byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(_Password));

                // Step 2. Create a new TripleDESCryptoServiceProvider object
                using (TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider())
                {
                    // Step 3. Setup the encoder
                    TDESAlgorithm.Key = TDESKey;
                    TDESAlgorithm.Mode = CipherMode.ECB;
                    TDESAlgorithm.Padding = PaddingMode.PKCS7;

                    // Step 4. Convert the input string to a byte[]
                    byte[] DataToEncrypt = UTF8.GetBytes(Message);

                    // Step 5. Attempt to encrypt the string
                    using (ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor())
                    {
                        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
                    }
                }
            }

            // Step 6. Return the encrypted string as a base64 encoded string
            return Convert.ToBase64String(Results);
        }

        public static string Decrypt(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below
            using (MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider())
            {
                byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(_Password));

                // Step 2. Create a new TripleDESCryptoServiceProvider object
                using (TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider())
                {
                    // Step 3. Setup the decoder
                    TDESAlgorithm.Key = TDESKey;
                    TDESAlgorithm.Mode = CipherMode.ECB;
                    TDESAlgorithm.Padding = PaddingMode.PKCS7;

                    // Step 4. Convert the input string to a byte[]
                    byte[] DataToDecrypt = Convert.FromBase64String(Message);

                    // Step 5. Attempt to decrypt the string
                    using (ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor())
                    {
                        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
                    }
                }
            }
            // Step 6. Return the decrypted string in UTF8 format
            return UTF8.GetString(Results);
        }

        public static string RsaEncrypt(string message, string publicKey)
        {
            System.Text.UTF8Encoding ByteConverter = new System.Text.UTF8Encoding();

            byte[] dataToEncrypt = ByteConverter.GetBytes(message);
            byte[] encryptedData;

            using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
            {
                RSAalg.FromXmlString(publicKey);
                encryptedData = RSAalg.Encrypt(dataToEncrypt, false);
            }

            return Convert.ToBase64String(encryptedData);
        }

        public static string RsaDecrypt(string message)
        {
            System.Text.UTF8Encoding ByteConverter = new System.Text.UTF8Encoding();

            byte[] encryptedData = Convert.FromBase64String(message);
            byte[] decryptedData;

            CspParameters cp = new CspParameters();
            cp.KeyContainerName = "ADAMSRSA";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp))
            {
                decryptedData = rsa.Decrypt(encryptedData, false);
            }

            return ByteConverter.GetString(decryptedData);
        }

        public static string RsaDecrypt2(string message, string privateKey)
        {
            System.Text.UTF8Encoding ByteConverter = new System.Text.UTF8Encoding();

            byte[] encryptedData = Convert.FromBase64String(message);
            byte[] decryptedData;

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(privateKey);
                decryptedData = rsa.Decrypt(encryptedData, false);
            }

            return ByteConverter.GetString(decryptedData);
        }
    }
}